﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace ApocDev.TownsApi.XmlFiles
{
    public class LivingEntityXml : ObservableCollection<LivingEntity>, IChangedXml
    {
        // Unfortunately, this is one of those files where things have to be "specially" handled to allow
        // people to create their own things.
        public LivingEntityXml(string actionsPath)
        {
            // Need to set this, bit hackish, but whatever!
            ModFiles.LivingEntities = this;

            XElement root = XElement.Load(actionsPath);
            IEnumerable<XElement> elements = root.Elements();
            int total = elements.Count();
            ModFiles.FireLoadEvent(this, new FileLoadEventArgs("LivingEntities", total, 0));
            int numLoaded = 0;
            foreach (XElement xml in elements)
            {
                Add(new LivingEntity(xml));
                ModFiles.FireLoadEvent(this, new FileLoadEventArgs("LivingEntities", total, ++numLoaded));
            }
        }

        public bool IsDirty { get { return this.Any(a => a.IsDirty); } }

        public XElement SaveDirty()
        {
            if (!IsDirty)
                return null;
            var actions = new XElement("livingentities");
            foreach (var a in this)
            {
                if (a.IsDirty)
                    actions.Add(a.Save());
            }
            return actions;
        }

        public XElement Save()
        {
            var actions = new XElement("livingentities");
            foreach (var a in this)
            {
                actions.Add(a.Save());
            }
            return actions;
        }
    }

    public class LivingEntity:IChangedXml
    {
        public override string ToString()
        {
            return ID;
        }
        public LivingEntity(XElement xml)
        {
            ID = xml.Name.LocalName;
        }

        public string ID { get; set; }
        public string Type { get; set; }
        public string Hate { get; set; }

        public List<string> AltGraphics { get; set; }
        public string NamePoolTag { get; set; }
        public string SurnamePoolTag { get; set; }
        public RandomRoll Attack { get; set; }
        public int AttackSpeed { get; set; }
        public RandomRoll HealthPoints { get; set; }
        public string AttackVerb { get; set; }
        public string AttackVerbInfinitive { get; set; }
        public RandomRoll Damage { get; set; }
        public RandomRoll LOS { get; set; }

        public Action Action { get; set; }

        public RandomRoll MovePCT { get; set; }
        public int WalkSpeed { get; set; }
        public RandomRoll MaxHungryTurns { get; set; }
        public RandomRoll MaxSleepTurns { get; set; }
        public List<Zone> EatZone { get; set; }

        public bool FacingDirections { get; set; }

        //<equipAllowed>metal,cloth,wood,bone,bow,hammer,spear,mace,sword,axe,misc</equipAllowed>
        public List<string> EquipAllowed { get; set; }

        public List<Drop> Drops { get; set; }

        public int HealthPointRegeneration { get; set; }

        // Heros
        public string HeroComePrerequisite { get; set; }
        public string HeroStayPrerequisite { get; set; }
        public string HeroBehavior { get; set; }
        public string HeroSkills { get; set; }
        public Item Equip { get; set; }
        public bool EvadeTraps { get; set; }
        public RandomRoll Moral { get; set; }

        // sips skele
        public LivingEntity FollowEntity { get; set; }
        public int FollowTurns { get; set; }

        // Caravan
        public string Caravan { get; set; }


        // fxdeadmale
        public string FxDead { get; set; }


        // aMiNals
        public Item FoodNeeded { get; set; }
        public RandomRoll FoodNeededTurns { get; set; }
        public RandomRoll FoodNeededDieTurns { get; set; }
        public RandomRoll MaxAge { get; set; }
        public RandomRoll MaxAgeLiving { get; set; }

        public List<Terrain> Habitats { get; set; }


        public class Drop
        {
            public Item Item { get; set; }
            public int Percent { get; set; }
        }

        public bool IsDirty { get; private set; }
        public XElement Save()
        {
            throw new NotImplementedException();
        }

        public XElement SaveDirty()
        {
            throw new NotImplementedException();
        }
    }


}
